﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ComUIControl
{
    public partial class RectangeBox : UserControl
    {
        private string _rectangeName = "name";
        public string RangeName
        {
            get
            {
                return _rectangeName;
            }
            set
            {
                labelName.Text = value;
                _rectangeName = value;
            }
        }

        private Rectangle _value;
        public Rectangle Value
        {
            get
            {
                return _value;
            }
            set
            {
                textBoxX.Text = value.X.ToString();
                textBoxY.Text = value.Y.ToString();
                textBoxHeight.Text = value.Height.ToString();
                textBoxWidth.Text = value.Width.ToString();
                _value = value;
            }
        }

        public RectangeBox()
        {
            InitializeComponent();           
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public delegate void UpdatePropertyDelegate(string strName);
        private void RaisePropertyChanged(string propertyName)
        {
            if (this.InvokeRequired)
            {
                UpdatePropertyDelegate outdelegate = new UpdatePropertyDelegate(RaisePropertyChanged);
                this.BeginInvoke(outdelegate, new object[] { propertyName });
                return;
            }
            var handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private void textBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\b'
                || Char.IsDigit(e.KeyChar)
                || Char.IsControl(e.KeyChar)
                || e.KeyChar == 46)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void textBoxX_Validating(object sender, CancelEventArgs e)
        {
            int val;
            if (int.TryParse(textBoxX.Text, out val))
            {
                if (val < 0 || val > 99999999)
                {
                    string infoErr = "请输入一个[0,99999999]范围内的数字";
                    errorProvider1.SetError(textBoxX, infoErr);
                }
                else
                {
                    errorProvider1.SetError(textBoxX, "");
                    if (_value.X != val)
                    {
                        _value.X = val;                       
                        Value = _value;
                    }
                }
            }
            else
            {
                errorProvider1.SetError(textBoxX, "不是合法的数字!");
                textBoxX.Focus();
            }
        }

        private void textBoxY_Validating(object sender, CancelEventArgs e)
        {
            int val;
            if (int.TryParse(textBoxY.Text, out val))
            {
                if (val < 0 || val > 99999999)
                {
                    string infoErr = "请输入一个[0,99999999]范围内的数字";
                    errorProvider1.SetError(textBoxY, infoErr);
                }
                else
                {
                    errorProvider1.SetError(textBoxY, "");
                    if (_value.Y != val)
                    {
                        _value.Y = val;
                        Value = _value;
                    }
                }
            }
            else
            {
                errorProvider1.SetError(textBoxY, "不是合法的数字!");
                textBoxY.Focus();
            }
        }

        private void textBoxWidth_Validating(object sender, CancelEventArgs e)
        {
            int val;
            if (int.TryParse(textBoxWidth.Text, out val))
            {
                if (val < 0 || val > 99999999)
                {
                    string infoErr = "请输入一个[0,99999999]范围内的数字";
                    errorProvider1.SetError(textBoxWidth, infoErr);
                }
                else
                {
                    errorProvider1.SetError(textBoxWidth, "");
                    if (_value.Width != val)
                    {
                        _value.Width = val;
                        Value = _value;
                    }
                }
            }
            else
            {
                errorProvider1.SetError(textBoxWidth, "不是合法的数字!");
                textBoxWidth.Focus();
            }
        }

        private void textBoxHeight_Validating(object sender, CancelEventArgs e)
        {
            int val;
            if (int.TryParse(textBoxHeight.Text, out val))
            {
                if (val < 0 || val > 99999999)
                {
                    string infoErr = "请输入一个[0,99999999]范围内的数字";
                    errorProvider1.SetError(textBoxHeight, infoErr);
                }
                else
                {
                    errorProvider1.SetError(textBoxHeight, "");
                    if (_value.Height != val)
                    {
                        _value.Height = val;
                        Value = _value;
                    }
                }
            }
            else
            {
                errorProvider1.SetError(textBoxHeight, "不是合法的数字!");
                textBoxHeight.Focus();
            }
        }
    }
}
